<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input @clear="serarchGoods" placeholder="请输入内容"   v-model="queryInfo.query" :clearable="true">
            <el-button slot="append" icon="el-icon-search" @click="serarchGoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8" class="addbtn">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 数据表格区域 -->
      <el-table border stripe :data="goodsList">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" width="110px" label="商品价格(元)"></el-table-column>
        <el-table-column prop="goods_weight" width="75px" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" width="170px" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time|getDate}}
          </template>
        </el-table-column>
        <el-table-column width="140px" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editGoods(scope.row.goods_id)" size="mini"></el-button>
            <el-button @click="deleteGoods(scope.row.goods_id)" size="mini" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5,10,15,20,40]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 总商品条数
      total: 0,
      // 商品表格数据源
      goodsList: []
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取全局商品数据
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('商品列表获取失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 搜索出来的商品
    serarchGoods () {
      this.getGoodsList()
    },
    // 删除商品
    async deleteGoods (id) {
      const confirm = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (confirm !== 'confirm') {
        return this.$message.info('取消删除成功')
      }
      // 你想删除
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.$message.success('删除商品成功')
      this.getGoodsList()
    },
    // 添加商品
    addGoods () {
      this.$router.push('/goods/add')
    },
    // 修改商品
    async editGoods (id) {
      const { data: res } = await this.$http.get('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('编辑失败')
      console.log(res.data)
      this.$router.push({ path: '/goods/add', params: { id: 123 } })
    }
  }
}
</script>
<style lang="less" scoped>
.addbtn {
  margin-left: 10px;
}
</style>
